/*
 * jQuery图片轮播(焦点图)插件
 * ADD.JENA.201206291027
 * EDIT.JENA.201206300904
 * EDIT.JENA.201207051027
 * EDIT.JENA.201208090849
 * EDIT.JENA.201303141312
 * Version: 1.2.0314
 * Author: jena
 * Demo: http://ishere.cn/demo/jquery.slidebox/
 */
(function ($) {
    $.slideBox = function (options, element) {
        //合并参数
        var settings = $.extend(true, {}, $.slideBox.defaults, options || {});
        //计算相关数据
        var wrapper = $(element), ul = wrapper.children('ul.items'), lis = ul.find('li');
        var firstPic = null;
        if (!settings.isHtmlPage) firstPic = lis.first().find('img');
        var li_num = lis.size(), li_height = 0, li_width = 0;
        //定义滚动顺序:ASC/DESC.ADD.JENA.201208081718
        var order_by = 'ASC';
        //初始化
        var init = function () {
            if (!wrapper.size()) return false;
            //手动设定值优先.ADD.JENA.201303141309
            li_height = settings.height ? settings.height : lis.first().height();
            li_width = settings.width ? settings.width : lis.first().width();

            wrapper.css({ width: li_width + 'px', height: li_height + 'px' });
            lis.css({ width: li_width + 'px', height: li_height + 'px' });//ADD.JENA.201207051027

            if (settings.direction == 'left') {
                ul.css('width', li_num * li_width + 'px');
            } else {
                ul.css('height', li_num * li_height + 'px');
            }
            ul.find('li:eq(' + settings.startIndex + ')').addClass('active');

            if (!settings.hideBottomBar) {//ADD.JENA.201208090859
                var tips = $('<div class="tips"></div>').css('opacity', settings.opacity).css('background-color', settings.backColor).appendTo(wrapper);
                if (!settings.isHtmlPage) {
                    var title = $('<div class="title"></div>').html(function () {
                        var active = ul.find('li.active').find('a'), text = active.attr('title'), href = active.attr('href');
                        return $('<a>').attr('href', href).text(text);
                    }).appendTo(tips);
                }
                var nums = $('<div class="nums"></div>').hide().appendTo(tips);
                lis.each(function (i, n) {
                    var a = null, text = '', href = '#', css = '', liA = $('<a>');
                    if (!settings.isHtmlPage) {
                        a = $(n).find('a');
                        text = a.attr('title');
                        href = a.attr('href');
                        liA.attr('href', href).text(text);
                    }
                    i == settings.startIndex && (css = 'active');
                    liA.addClass(css).css('borderRadius', settings.clickBarRadius + 'px').mouseover(function () {
                        $(this).addClass('active').siblings().removeClass('active');
                        ul.find('li:eq(' + $(this).index() + ')').addClass('active').siblings().removeClass('active');
                        start();
                        stop();
                    }).appendTo(nums);
                });
                if (settings.pagerStyle == 'left') {
                    nums.css('left', 0);
                } else if (settings.pagerStyle == 'right') {
                    nums.css('right', 0);
                } else if (settings.pagerStyle == 'center') {
                    var left = (li_width - nums.width()) / 2;
                    nums.css('left', left + 'px');
                }

                if (settings.hideClickBar) {//ADD.JENA.201206300847
                    tips.hover(function () {
                        nums.animate({ top: '0px' }, 'fast');
                    }, function () {
                        nums.animate({ top: tips.height() + 'px' }, 'fast');
                    });
                    nums.show().delay(2000).animate({ top: tips.height() + 'px' }, 'fast');
                } else {
                    nums.show();
                }
            }

            lis.size() > 1 && start();
        };
        //开始轮播
        var start = function () {
            var active = ul.find('li.active'), active_a;
            if (!settings.isHtmlPage) active_a = active.find('a');

            var index = active.index();
            if (settings.direction == 'left') {
                offset = index * li_width * -1;
                param = { 'left': offset + 'px' };
            } else {
                offset = index * li_height * -1;
                param = { 'top': offset + 'px' };
            }

            wrapper.find('.nums').find('a:eq(' + index + ')').addClass('active').siblings().removeClass('active');
            if (!settings.isHtmlPage) {
                wrapper.find('.title').find('a').attr('href', active_a.attr('href')).text(active_a.attr('title'));
            }
            ul.stop().animate(param, settings.duration * 1000, settings.easing, function () {
                active.removeClass('active');
                if (order_by == 'ASC') {
                    if (active.next().size()) {
                        active.next().addClass('active');
                    } else {
                        order_by = 'DESC';
                        active.prev().addClass('active');
                    }
                } else if (order_by == 'DESC') {
                    if (active.prev().size()) {
                        active.prev().addClass('active');
                    } else {
                        order_by = 'ASC';
                        active.next().addClass('active');
                    }
                }
            });
            wrapper.data('timeid', window.setTimeout(start, settings.delay * 1000));
        };
        //停止轮播
        var stop = function () {
            window.clearTimeout(wrapper.data('timeid'));
        };
        //鼠标经过事件
        wrapper.hover(function () {
            stop();
        }, function () {
            wrapper.data('timeid', window.setTimeout(start, settings.delay * 1000));//ADD.JENA.201303141309
        });
        if (!settings.isHtmlPage) {
            //首张图片加载完毕后执行初始化
            var imgLoader = new Image();
            imgLoader.onload = function () {
                imgLoader.onload = null;
                init();
            };
            imgLoader.src = firstPic.attr('src');
        } else {
            init();
        }
    };
    //默认参数
    $.slideBox.defaults = {
        direction: 'left',//left,top
        duration: 0.6,//unit:seconds
        easing: 'swing',//swing,linear
        delay: 3,//unit:seconds
        startIndex: 0,
        hideClickBar: true,
        clickBarRadius: 5,//unit:px
        hideBottomBar: false,
        width: null,
        height: null,
        isHtmlPage: false, // 是否内容页 ADD.ALEX.20170817201300
        pagerStyle: 'right', // 切换器的显示位置 ADD.ALEX.20170817201300
        opacity: 0.6, //底部透明度
        backColor: '#000' //底部背景色
    };

	$.fn.slideBox = function(options) {
	    return this.each(function () {
	        new $.slideBox(options, this);
	    });
	};
})(jQuery);
